package com.jimiopen.common.utils.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

public class FileManager {

	private String file;

	public FileManager(String file) {
		this.file = file;
	}

	private static final Logger log = LoggerFactory
			.getLogger(FileManager.class);

	public static int FileOutput(String dir, String fileName, String data)
			throws IOException {
		FileManager manager = null;
		if (StringUtils.isBlank(fileName)) {
			log.info("null value of fileName");
			return 0;
		} else if (StringUtils.isBlank(data)) {
			log.info("null value of data");
			return 0;
		} else if (StringUtils.isBlank(dir)) {
			log.info("null value of dir");
			manager = new FileManager("D:" + File.separator + "Workspaces"
					+ File.separator + "lucene-trunk" + File.separator + "web"
					+ File.separator + "WEB-INF" + File.separator + "files"
					+ File.separator + fileName);
		} else {
			manager = new FileManager(dir + fileName);
		}
		log.info("####" + JSON.toJSONString(manager));
		File file = new File(manager.getFile());
		if (!file.exists()) {
			file.createNewFile();
		}
		FileWriter fileWritter;
		log.info("####" + JSON.toJSONString(file));
		fileWritter = new FileWriter(file.getName(), true);
		BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
		bufferWritter.write(data);
		bufferWritter.close();
		return 1;
	}

	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}
}
